package com.yuma.function4;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class ConsumerDemo {
    public static void main(String[] args) {
        // 消费者就消费此参数  匿名内部类
        Consumer<String> c = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        c.accept("hello world");

        Consumer<String> c2 = (String s) -> {
            System.out.println(s);
        };
        c2.accept("hello world2");

        // lambda -> (参数名)->返回值;
        Consumer<String> c3 = (s) -> System.out.println(s);
        c3.accept("hello world3");

        // Consumer<? super T>    // 泛型的下限，元素只能是String的对象和String父类对象
        List<String>  list = List.of("a","b","c");
        list.forEach(s->System.out.println(s));
        list.forEach(c3);

        Map<String,String> map = new HashMap<>();
        map.put("1","a");
        map.put("2","b");
        map.put("3","c");
        map.put("4","d");
        map.put("5","e");
        map.forEach((k,v)->System.out.printf("key=%s,value=%s\n",k,v));

    }
}
